/**
 * @author casazaft
 */
Package("j2j.effect").Class("Slideable", {
    Slideable: function(config){
    
    },
    Prototype: {
        extendController: function(config){
            var Y1, Y2, ofY;
            var g1, g2, ofg;
            var dom = this.dom;
            var egr = Import("j2j.event.EventMgr");
            var geo = Import("j2j.dom.Geometry");
            
            var task;
            var mutex = {
                running: false
            };
            var slideScroll = function(s, fn, e){
                task = new xclab.utils.Task({
                    fn: function(v, v0){
                        this.v = this.v0 / this.t;
                        fn(0, this.v);
                        this.t++;
                    },
                    time: config.time || 0.5,
                    fps: config.fps || 25,
                    v: 0,
                    v0: s / 4,
                    t: 1,
                    mutex: mutex
                });
                task.run();
                ofg = 0;
            };
            
            
            var domMoveHandler = function(e){
                e = e || event;
                Y2 = e.clientY;
                ofY = Y1 - Y2;
                scrollBy(0, ofY);
                Y1 = e.clientY;
                ofg = g1 - Y2;
                egr.stopEvent(e);
            };
            
            var domDownHandler = function(e){
                e = e || event;
                Y1 = e.clientY;
                g1 = e.clientY;
                if (task) 
                    task.stop();
                
                egr.addEventListener(dom, "mouseup", domUpHandler, false);
                egr.addEventListener(document, "mousemove", domMoveHandler, false);
                egr.removeEventListener(dom, "mousedown", domDownHandler, false);
                egr.stopEvent(e);
            };
            
            var domUpHandler = function(e){
                e = e || event;
                g2 = e.clientY;
                Y1 = e.clientY;
                ofg = g1 - g2;
                slideScroll(ofg, scrollBy, e);
                
                egr.removeEventListener(dom, "mouseup", domUpHandler, false);
                egr.removeEventListener(document, "mousemove", domMoveHandler, false);
                egr.addEventListener(dom, "mousedown", domDownHandler, false);
                egr.stopEvent(e);
                
            };
            var w = geo.getViewportWidth();
            var h = geo.getViewportHeight();
            var domOutHandler = function(e){
                var x = e.clientX;
                var y = e.clientY;
                if (!(x > 0 && x < w && y > 0 && y < h)) {
                
                    if (task) 
                        task.stop();
                    slideScroll(ofg, scrollBy, e);
                    egr.removeEventListener(dom, "mouseup", domUpHandler, false);
                    egr.removeEventListener(document, "mousemove", domMoveHandler, false);
                    egr.addEventListener(dom, "mousedown", domDownHandler, false);
                    egr.stopEvent(e);
                }
            };
            this.finalize = function(){
            
                egr.removeEventListener(dom, "mouseup", domUpHandler, false);
                egr.removeEventListener(document, "mousemove", domMoveHandler, false);
                egr.removeEventListener(dom, "mousedown", domDownHandler, false);
                dom = null;
                this.Super.finalize(this);
            };
            this.getDom = function(){
                return query.exec()[0];
            };
            egr.addEventListener(dom, "mousedown", domDownHandler, false);
            egr.addEventListener(document, "mouseout", domOutHandler, false);
        }
    },
    Extends: j2j.patterns.Decorator
});
